from __future__ import with_statement
import pickle
import datetime

class Message:
  def __init__(self, to, sender, subject, id, date, data):
    self.to = to
    self.sender = sender
    self.id = id
    self.date = date
    self.data = data

class MessageDeliveryReport:  
  def __init__(self, message):
    self.to = message.to
    self.sender = message.sender
    self.subject = message.subject
    self.id = message.id
    self.date = datetime.datetime.now()

class DataStore:
  def __init__(self, messages = {}, delivery_reports = {}, delivered_messages = {}):
    self.messages = messages
    self.delivery_reports = delivery_reports
    self.delivered_messages = delivered_messages
    
  def add_message(self, message):
    self.messages[message.id] = message
  
  def deliver_message(self, message):
    del self.messages[message.id]
    report = MessageDeliveryReport(message)
    self.delivered_messages[message.id] = message
    self.delivery_reports[report.id] = report
    return report
  
  def save(self, path_prefix = '', filename = 'datastore'):
    with open(path_prefix + filename, "w") as f:
      pickle.dump(self, f)

def load(path_prefix = '', filename = 'datastore'):
  with open(path_prefix + filename, "r") as f:
    return pickle.load(f)
